var OrgList = function() {

	var left_margin_step = 15;
	
	$("#orgName").bind(
			'click',
			function() {
				var myts = new Date();
				$("#org_filter").val("");
				var formStr = $(this).parents("div").find("#org_filter").serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context + "/register/qryGroup?myts=" + myts,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
						if(result != "[]"){
							var orgArr = eval(result);
							$("#groupUnitPicker").find(".tree_list").empty();
							for ( var loop = 0; loop < orgArr.length; loop++) {
								var obj = orgArr[loop];
								var a = $("<a></a>").addClass(
										"category_trigger").attr("margin", 0);
								var inputObj = $("<input/>").val(obj.pk_org).attr(
										"type", "radio").attr("name", "pk_org_intree");
								if(obj.isRegistered == "N"){
									inputObj.attr("disabled", true);
								}
								var label = $("<label></label>").text(obj.name);
								var inputObj1 = $("<input/>").val(obj.pk_group)
										.attr("type", "hidden").attr("name",
												"pk_group_intree");
								var div = $("<div></div>")
										.addClass("tree_category").append(a)
										.append(inputObj).append(label).append(
												inputObj1);
								$("#groupUnitPicker").find(".tree_list")
										.append(div);
							}
						}
					}
				});
				
				var position = $("#orgName").offset();
				$("#groupUnitPicker").slideDown("normal");
				$("#groupUnitPicker").offset(position);
				
	});

	$("#filterPurOrg").bind(
			'click',
			function() {
				var myts = new Date();
				var formStr = $(this).parents("div").find("#org_filter").serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context + "/register/qryGroup?myts=" + myts,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
						$("#groupUnitPicker").find(".tree_list").empty();
						if(result != "[]"){
							var orgArr = eval(result);
							for ( var loop = 0; loop < orgArr.length; loop++) {
								var obj = orgArr[loop];
								var a = $("<a></a>").addClass(
										"category_trigger").attr("margin", 0);
								var inputObj = $("<input/>").val(obj.pk_org).attr(
										"type", "radio").attr("name", "pk_org_intree");
								if(obj.isRegistered == "N"){
									inputObj.attr("disabled", true);
								}
								var label = $("<label></label>").text(obj.name);
								var inputObj1 = $("<input/>").val(obj.pk_group)
										.attr("type", "hidden").attr("name",
												"pk_group_intree");
								var div = $("<div></div>")
										.addClass("tree_category")
										.append(a)
										.append(inputObj).append(label).append(
												inputObj1);
								$("#groupUnitPicker").find(".tree_list")
										.append(div);
							}
						}
					}
				});
			});
			
	

	$(".category_trigger").live(
			'click',
			function() {
				var category_triggerObj=$(this);
				if(category_triggerObj.hasClass("close")){
					category_triggerObj.parent(".tree_category").next(".children").slideUp(function(){
						category_triggerObj.removeClass("close");
					});
					return false;
				}
				
				$(this).addClass("close");
				var pk_org_val = $(this).next("input").val();
				var pk_group_val = $(this).next("input").next("label").next("input").val();
				
				if(pk_org_val != pk_group_val){
					return ;
				}
				
				var myts = new Date();
				var formStr = $(this).parents("div").find("#org_filter").serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context
							+ "/register/qrySubPurchaseOrgByParentPk?pk_org="
							+ pk_org_val + "&pk_group=" + pk_group_val
							+ "&myts=" + myts,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
						var orgArr = eval(result);
						
						var current_margin_left_size = parseInt(category_triggerObj.attr("margin")) + left_margin_step;	
						
						var childrenDiv = $("<div></div>").addClass("children");
						for ( var loop = 0; loop < orgArr.length; loop++) {
							var obj = orgArr[loop];

							var a = $("<a></a>").addClass(
									"category_trigger").attr("style","margin-left:"+current_margin_left_size+"px").attr("margin", current_margin_left_size);
							var inputObj = $("<input/>").val(obj.pk_org).attr(
									"type", "radio").attr("name", "pk_org_intree");
							if(obj.isPurchaseOrg == "N"){
								inputObj.attr("disabled", "disabled");
							}
							var label = $("<label></label>").text(obj.name);
							var inputObj1 = $("<input/>").val(obj.pk_group)
									.attr("type", "hidden").attr("name",
											"pk_group_intree");
							var div = $("<div></div>")
									.addClass("tree_category")
									.append(a)
									.append(inputObj).append(label).append(
											inputObj1);
							childrenDiv.append(div);
						}
						childrenDiv.insertAfter(category_triggerObj.parent());
					}
				});
				
				 category_triggerObj.parent(".tree_category").siblings(".tree_category").next(".children").slideUp(function(){
					 $(this).prev(".tree_category").find(".category_trigger").removeClass("close");
				 });

				return false;

			});
	
	$("#groupPicked").live('click', function() {
		var items = $("#groupUnitPicker").find("[name='pk_org_intree']");
		if(items != null){
			for(var loop=0; loop<items.length; loop++){
				if(items[loop].checked == true){
					//如果原值为空，则直接赋值为所选值
					if($("#vendorOrg").val()==""){
						$("#vendorOrg").val($(items[loop]).val());
						$("#orgName").val($(items[loop]).next("label").html());
						if($("#orgName").val()!=""){
							$("#orgName").attr("style", "color:black");
						}
						$("#vendorGroup").val($(items[loop]).next().next("input").val());
						$("#pk_org").val($(items[loop]).val());
						$("#pk_group").val($(items[loop]).next().next("input").val());
						break;
					}else{ //如果此次选中组织与原来值相同，则什么都不做
						if($("#vendorOrg").val() ==  $(items[loop]).val()){
							break;
						}else{
							var option = {
									title : getLanMsgs("0ec22180-100163"),
									content : getLanMsgs("0ec22180-100164"),
									confirm : function() {
										$("#vendorOrg").val($(items[loop]).val());
										$("#orgName").val($(items[loop]).next("label").html());
										if($("#orgName").val()!=""){
											$("#orgName").attr("style", "color:black");
										}
										$("#vendorGroup").val($(items[loop]).next().next("input").val());
										$("#pk_org").val($(items[loop]).val());
										$("#pk_group").val($(items[loop]).next().next("input").val());
										//清空产品和资质及附件
										$("#productTable").find("tbody").find("tr").remove();
										$("#complateQualification").find(".qaITerms").remove();	
										return true; //表示关闭
								}
							};
							showConfirmDialog(option);
							break;
						}
					}
				}
			}
		}
		$("#groupUnitPicker").slideUp();
		//如果是港华，添加必输资料类型
		if(bGh2Version){
			var reqdoctype = $("#complateDocType").find(".reqdoctype_tpl").length;
			if(!reqdoctype){			//如果未添加过必输资料类型
				addRequiredDocType();
			}
		}
	});
	
	$("#groupCancel").live('click', function() {
		$("#groupUnitPicker").slideUp();
	});

	
	this.init = function() {

	};
	
	
}

$(function() {
	var orgListInstance = new OrgList();
	orgListInstance.init();
});

//港华新加 当选择了所属组织之后添加必输的文档类型 
function addRequiredDocType() {
	var myts = new Date();
	var formStr = $('#doctype_filter').serialize();
	var pk_org = $("#vendorOrg").val();
	var pk_group = $("#vendorGroup").val();
	if((pk_org==null||pk_org=="")){
		showCfmMsg(getLanMsgs("0ec22180-100163"), getLanMsgs("0ec22180-100165"));
		return;
	}
	jQuery.ajax( {
		type : "GET",
		async : false,
		url : ebvp_context + "/register/getRequiredDocType?myts="+myts+"&pk_group="+pk_group+"&pk_org="+pk_org,
		dataType : "text",
		data : "&" + formStr,
		success : function(result) {
			if (result != null && result != "[]") {
				var arr = eval(result);
				for (var loop = 0; loop < arr.length; loop++) {
					var newrow = $("#reqdoctype_tpl").find(".idItems").clone();
					var pk = arr[loop].pk_defdoc;
					var name = arr[loop].name;
					$(newrow.find("[name='finfotype']")[0]).val(pk);
					$(newrow.find("[name='finfotypeName']")[0]).val(name);
					$(newrow.find("[name='addDocTypeAttachment']")[0]).attr("id", "addDocTypeAttachment_" + pk);
					newrow.insertBefore($("#addDocType"));
				}
			}
		}
});
}
